﻿<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
	<xsl:strip-space elements="*" />
	<!--Get the root element-->
	<xsl:template match="/">
		<xsl:for-each select="//FormCollection">
			<xsl:call-template name="HeaderTemplate" />
		</xsl:for-each>
	</xsl:template>
	<xsl:template name="HeaderTemplate">
		<fo:root>
			<!--Set the page layout-->
			<fo:layout-master-set>
				<fo:simple-page-master master-name='page' margin='0.5cm'  page-width='210mm' page-height='297mm'>
					<fo:region-body margin='0.5cm' margin-bottom='1.0cm' margin-top='1.5cm' region-name='body' />
					<fo:region-after extent='1.0cm' region-name="footer" />
					<fo:region-before region-name="header" />
					<fo:region-start extent='0.5cm' />
					<fo:region-end extent='0.5cm' />
				</fo:simple-page-master>
			</fo:layout-master-set>
			<!--page flow-->
			<fo:page-sequence master-reference="page">
				<!--Page footer creation(Note:This section will be avaialable on all the pages of the output)-->
				<fo:static-content flow-name='footer'>
					<fo:table>
						<fo:table-body>
							<fo:table-row>
								<fo:table-cell border='1pt' padding='1pt'>
									<fo:block text-align='center'>Electronic COI Disclosure</fo:block>
								</fo:table-cell>
							</fo:table-row>
							<fo:table-row>
								<fo:table-cell border='1pt' padding='1pt'>
									<fo:block text-align='center'>Page <fo:page-number /> of <fo:page-number-citation ref-id='last-page' /></fo:block>
								</fo:table-cell>
							</fo:table-row>
						</fo:table-body>
					</fo:table>
				</fo:static-content>
				<!--Page Header creation(Note:This section will be avaialable on all the pages of the output)-->
				<fo:static-content flow-name='header'>
					<fo:block text-align='right'>
						<xsl:variable name='Logo'>
							<xsl:value-of select="../LogoLocation" />
						</xsl:variable>
						<fo:external-graphic scaling='uniform' content-width="100%" content-height="100%" src="url({$Logo})" />
					</fo:block>
				</fo:static-content>
				<!--Body of the page starts here-->
				<fo:flow flow-name='body'>
					<!--Add protocol detail to the output(This section goes in the top of the first page-->
					<fo:block linefeed-treatment='preserve' white-space-treatment='preserve' white-space-collapse='false'
						font-family='times new roman' font-size='10pt' />
					<fo:block space-before='0.5cm' />
					<fo:table>
						<fo:table-body>
							<fo:table-row>
								<fo:table-cell border='1pt' padding='1pt'>
									<fo:block font-family='Helvetica' font-size='12pt' text-align='left'>COI Disclosure<fo:inline font-weight='bold'></fo:inline></fo:block>
								</fo:table-cell>
							</fo:table-row>
						</fo:table-body>
					</fo:table>
					<!--Add each answered form(Questionnaire to the output)-->
					<xsl:for-each select="Form">
						<fo:block space-before='0.5cm' />
						<!--if the form is not marked as removed then display the <form name> with the background -->
						<xsl:if test="@Remove='false'">
							<fo:table>
								<fo:table-body>
									<fo:table-row>
										<fo:table-cell border='1pt white' padding='2pt'>
											<fo:block font-family='Verdana, Arial, Helvetica, sans-serif' font-size='12pt' font-weight='bold'
												text-align='left' background-color="#dddddd">
												<xsl:if test="@AllowMultiple='true'">
													<xsl:value-of select="@Name" />
													<fo:inline>: </fo:inline>
												</xsl:if>
												<xsl:value-of select="@Answer"></xsl:value-of>
											</fo:block>
										</fo:table-cell>
									</fo:table-row>
								</fo:table-body>
							</fo:table>
							<!--Iterate through formItems to display it-->
							<xsl:for-each select="FormItems/FormItem">
								<xsl:call-template name="ItemTemplate"></xsl:call-template>
							</xsl:for-each>
						</xsl:if>
						<!--if the form is marked as removed then display a message <form name> was removed -->
						<xsl:if test="@Remove='true'">
							<fo:table>
								<fo:table-body>
									<fo:table-row>
										<fo:table-cell border='1pt white' padding='2pt'>
											<fo:block font-family='Verdana, Arial, Helvetica, sans-serif' font-size='12pt' font-weight='bold'
												text-align='left' background-color="#dddddd">
												<xsl:value-of select="@Name" />
												<fo:inline>: </fo:inline>
												<xsl:value-of select="@Answer" />
												<fo:inline> was removed from the study</fo:inline>
											</fo:block>
										</fo:table-cell>
									</fo:table-row>
								</fo:table-body>
							</fo:table>
						</xsl:if>
					</xsl:for-each>
					
					<fo:block id='last-page' />
				</fo:flow>
			</fo:page-sequence>
		</fo:root>
	</xsl:template>
	<!--Display each item in the form-->
	<xsl:template name="ItemTemplate">
		<fo:block text-align="center">
			<xsl:if test="HasSeparator='true'">
				<!-- Separate the rows with horizontal line -->
				<fo:leader leader-pattern="rule" alignment-baseline="middle" leader-length="7.3in" rule-thickness="0.5pt"
					color="black" />
			</xsl:if>
		</fo:block>
		<!--Add the Label(form item) content to the output-->
		<xsl:variable name="type">
			<xsl:value-of select="Type" />
		</xsl:variable>
		<fo:block margin-left='0.5cm' font-family="Times new roman" font-size='12pt'>
			<xsl:variable name="htmlStrippedText">
				<xsl:value-of select="HtmlStrippedText" />
			</xsl:variable>
			<xsl:if test="not($htmlStrippedText='')">
				<fo:block space-before='0.2cm' />
				<xsl:if test="$type='Label'">
					<xsl:if test="ItemProperties/ItemProperty/AttributeValue='True'">
						<fo:block font-size='12pt' font-weight='bold'> <!--margin-left='0.75cm' font-family="Times new roman"-->
							<xsl:value-of select='$htmlStrippedText' />
						</fo:block>
					</xsl:if>
					<xsl:if test="not(ItemProperties/ItemProperty/AttributeValue='True')">
						<!--<xsl:value-of select='$htmlStrippedText' />-->
						<xsl:value-of select="translate($htmlStrippedText,'-','&#x2028;')"/>
					</xsl:if>
				</xsl:if>
				<xsl:if test="not($type='Label')">
					<xsl:value-of select='$htmlStrippedText' />
				</xsl:if>
			</xsl:if>
		</fo:block>
		<!--<fo:block space-before='0.2cm'/>-->
		<xsl:choose>
			<!--Add the Textbox(form item) content to the output-->
			<xsl:when test="$type='TextBox'">
				<xsl:for-each select="./ItemProperties/ItemProperty">
					<xsl:if test="AttributeValue ='SingleLine'">
						<fo:block text-align="Right" />
					</xsl:if>
				</xsl:for-each>
				<fo:block space-before='0.2cm' />
				<fo:block margin-left='0.75cm' font-family="verdana" font-size='12pt' text-align="Right">
					<xsl:value-of select="Answers/Answer/Answer" />
				</fo:block>
			</xsl:when>
			<!--Add the Radiobuttonlist or DropDownlist or CheckBoxList (form item) content to the output-->
			<xsl:when test="$type='RadioButtonList' or $type='DropDownList' or $type='CheckBoxList'">
				<xsl:for-each select="./ItemOptions/ItemOption">
					<xsl:variable name="optionValueVariable">
						<xsl:value-of select="OptionValue" />
					</xsl:variable>
					<xsl:if test="../../Answers/Answer[Answer = $optionValueVariable]/Selected = 'true'">
						<fo:block space-before='0.2cm' />
						<fo:block margin-left='0.75cm' font-family="verdana" font-size='12pt'>
							<xsl:value-of select="HtmlStrippedOptionText" />
						</fo:block>
						<xsl:for-each select="../../FormItems/FormItem[ParentValue = $optionValueVariable]">
							<xsl:if test="Indent='true'">
								<fo:block space-before='0.2cm' />
								<fo:block margin-left='0.75cm'>
									<xsl:call-template name="ItemTemplate"></xsl:call-template>
								</fo:block>
							</xsl:if>
							<xsl:if test="Indent='false'">
								<fo:block space-before='0.2cm' />
								<xsl:call-template name="ItemTemplate"></xsl:call-template>
							</xsl:if>
						</xsl:for-each>
					</xsl:if>
				</xsl:for-each>
			</xsl:when>
			<!--Add the MultiInput (form item) content to the output-->
			<xsl:when test="$type='MultiInput'">
				<xsl:for-each select="./Answers/Answer">
					<xsl:variable name="answerVariable">
						<xsl:value-of select="Answer" />
					</xsl:variable>
					<fo:block space-before='0.2cm' />
					<fo:block margin-left='0.75cm' font-family="verdana" font-size='12pt'>
						<xsl:value-of select="$answerVariable"></xsl:value-of>
					</fo:block>
					<xsl:if test="../../FormItems/FormItem[ParentValue = $answerVariable]">
						<xsl:for-each select="../../FormItems/FormItem[ParentValue = $answerVariable]">
							<xsl:if test="Indent='true'">
								<fo:block space-before='0.2cm' />
								<fo:block margin-left='0.75cm'>
									<xsl:call-template name="ItemTemplate"></xsl:call-template>
								</fo:block>
							</xsl:if>
							<xsl:if test="Indent='false'">
								<fo:block space-before='0.2cm' />
								<xsl:call-template name="ItemTemplate"></xsl:call-template>
							</xsl:if>
						</xsl:for-each>
					</xsl:if>
				</xsl:for-each>
			</xsl:when>
			<!--Add the DropDownMultiSelect (form item) content to the output-->
			<xsl:when test="$type='DropDownMultiSelect'">
				<xsl:for-each select="./Answers/Answer">
					<xsl:variable name="answerVariable">
						<xsl:value-of select="Answer" />
					</xsl:variable>
					<fo:block space-before='0.2cm' />
					<fo:block margin-left='0.75cm' font-family="verdana" font-size='12pt'>
						<xsl:value-of select="../../ItemOptions/ItemOption[OptionValue = $answerVariable]/HtmlStrippedOptionText" />
					</fo:block>
					<xsl:if test="../../FormItems/FormItem[ParentValue = $answerVariable]">
						<xsl:for-each select="../../FormItems/FormItem[ParentValue = $answerVariable]">
							<xsl:if test="Indent='true'">
								<fo:block space-before='0.2cm' />
								<fo:block margin-left='0.75cm'>
									<xsl:call-template name="ItemTemplate"></xsl:call-template>
								</fo:block>
							</xsl:if>
							<xsl:if test="Indent='false'">
								<fo:block space-before='0.2cm' />
								<xsl:call-template name="ItemTemplate"></xsl:call-template>
							</xsl:if>
						</xsl:for-each>
					</xsl:if>
				</xsl:for-each>
			</xsl:when>
		</xsl:choose>
	</xsl:template>
</xsl:stylesheet>
